//
//  PKProductMainViewController.m
//  PianKe
//
//  Created by lanou on 15/9/25.
//  Copyright (c) 2015年 lanou. All rights reserved.
//

#import "PKProductMainViewController.h"
#import "PKProductTableViewCell.h"
#import "PKRequestManager.h"
@interface PKProductMainViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)UITableView *mainTableView;//主页面
@property(nonatomic,strong)NSMutableArray *dataSourceArray;
@end

@implementation PKProductMainViewController
-(void)acquireData
{
    NSMutableDictionary *parDic=[NSMutableDictionary dictionary];
    parDic[@"start"] = @"0";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"limit"] = @"10";
    parDic[@"auth"] = @"";
    parDic[@"version"] = @"3.0.2";
    [PKRequestManager requestWithType:POST urlString:@"http://api2.pianke.me/pub/shop" patDic:parDic finish:^(NSData *data) {
        NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        NSArray *listArray =dic[@"data"][@"list"];
        for (NSDictionary *modelDict in  listArray) {
            PKProductMainListModel *model=[[PKProductMainListModel alloc]init];
            [model setValuesForKeysWithDictionary:modelDict];
            [self.dataSourceArray addObject:model];
        }
        [_mainTableView  reloadData];
    } Error:^(NSError *error) {
        NSLog(@"良品主页面网络请求失败:%@",error);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor blackColor];
    _mainTableView=[[UITableView alloc]initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _mainTableView.delegate=self;
    _mainTableView.dataSource=self;
//    _mainTableView.rowHeight=300;
    [_mainTableView registerClass:[PKProductTableViewCell class] forCellReuseIdentifier:@"PKProductTableViewCell"];
    [self.view addSubview:_mainTableView];
    _dataSourceArray=[NSMutableArray array];

    [self acquireData];
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataSourceArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    PKProductTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"PKProductTableViewCell" forIndexPath:indexPath];
    cell.configureModel=_dataSourceArray[indexPath.row];
    [cell.buyButton addTarget:self action:@selector(handleButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    cell.buyButton.tag=1000+indexPath.row;
    return cell;

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [PKProductTableViewCell heightForCell];
}
-(void)handleButtonAction:(UIButton *)sender
{
//    PKProductTableViewCell *cell =sender.superview.superview;
//    NSIndexPath *indexPath=[_mainTableView indexPathForCell:cell];
    PKProductMainListModel *model=_dataSourceArray[sender.tag-1000];
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:model.buyurl]];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
